package com.weilele.leisure.time

import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.annotation.DrawableRes
import androidx.annotation.RequiresApi
import com.weilele.leisure.time.ui.module.video_cache.VideoCacheActivity
import com.weilele.leisure.time.ui.module.video_cache.VideoCacheFragment
import com.weilele.mvvm.app
import com.weilele.mvvm.utils.shortcut.ShortcutBean
import com.weilele.mvvm.utils.shortcut.setShortsCuts

class ShortcutActivity : AppCompatActivity() {
    companion object {
        private const val KEY_CACHE_TYPE = "CACHE_VIDEO_ID"
        private const val CACHE_VIDEO_ID = "CACHE_VIDEO_ID"
        fun initShortcut() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
                setShortsCuts(
                    mutableListOf(
                        createShortCut(CACHE_VIDEO_ID, "缓存视频", R.drawable.icon_shortcut_cache)
                    )
                )
            }
        }

        @RequiresApi(Build.VERSION_CODES.N_MR1)
        private fun createShortCut(id: String, title: String, @DrawableRes icon: Int) =
            ShortcutBean(
                id,
                null,
                title,
                title,
                title,
                icon
            ) {
                it.setIntent(Intent(app, VideoCacheActivity::class.java).also { intent ->
                    intent.putExtra(KEY_CACHE_TYPE, CACHE_VIDEO_ID)
                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                    intent.action = Intent.ACTION_VIEW
                })
            }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        when (intent?.getStringExtra(KEY_CACHE_TYPE)) {
            CACHE_VIDEO_ID -> {
                VideoCacheFragment.start(this)
            }
            else -> {
            }
        }
        finish()
    }
}